home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_ghostscript.idb / usr / freeware / lib / ghostscript / 3.33 / packfile.ps.z / packfile.ps
Encoding:
Text File  |  1998-05-21  |  9.9 KB  |  329 lines

  1. %    Copyright (C) 1994, 1995 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of GNU Ghostscript.
  3. % GNU Ghostscript is distributed in the hope that it will be useful, but
  4. % WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility to
  5. % anyone for the consequences of using it or for whether it serves any
  6. % particular purpose or works at all, unless he says so in writing.  Refer
  7. % to the GNU Ghostscript General Public License for full details.
  8.  
  9. % packfile.ps
  10. % Pack groups of files together, with compression, for use in
  11. % storage-scarce environments.
  12.  
  13. % ****** NOTE: This file must be kept consistent with gs_pfile.ps.
  14.  
  15. % ---------------- Huffman coding utilities ---------------- %
  16.  
  17. % We count runs of zeros, and individual byte frequencies separately
  18. % depending on whether they follow or do not follow a run of zeros.
  19. /zruns 256 array def
  20. /zfreq 256 array def
  21. /nzfreq 256 array def
  22. /maxcode 13 def        % max code length, must be between 10 and 16
  23. /maxzrun 100 def    % max length of zero run, must be <= 100
  24. /statbuf 10000 string def
  25.  
  26. % Initialize statistics.
  27. /initstats        % - initstats -
  28.  { 0 1 255 { zruns exch 0 put } for
  29.    0 1 255 { zfreq exch 0 put } for
  30.    0 1 255 { nzfreq exch 0 put } for
  31.  } bind def
  32.  
  33. % Accumulate statistics from an individual file.
  34. /addstats        % <file> addstats -
  35.  { 0
  36.     { 1 index //statbuf readstring 3 1 roll
  37.     % Stack: file eof numzeros data
  38.        { dup 0 eq
  39.       { pop 1 add
  40.       }
  41.       { 1 index 0 ne
  42.          { exch 255 min
  43.            //zruns exch 2 copy get 1 add put
  44.            0 exch //zfreq
  45.          }
  46.          { //nzfreq
  47.          }
  48.         ifelse
  49.         exch 2 copy get 1 add put
  50.       }
  51.      ifelse
  52.        } forall
  53.       exch not { exit } if (.) print flush
  54.     } loop
  55.    pop closefile
  56.  } bind def
  57.  
  58. % Compute the Huffman codes from the statistics.
  59. /statcodes        % - statcodes <array>
  60.  { maxcode 1 add 256 add maxzrun 2 sub add 1 add array    % full array
  61.    dup maxcode 1 add dup 2 index length exch sub getinterval    % data
  62.     % Put statistics into array
  63.    dup 0 1 255
  64.     { zfreq 1 index get nzfreq 2 index get add put dup
  65.     } for
  66.    0 zruns 1 get put
  67.    256 zruns 2 maxzrun 2 sub getinterval putinterval
  68.    dup dup length 1 sub 1 put    % EOD
  69.    maxcode .computecodes
  70.  } bind def
  71.  
  72. % ---------------- File handling ---------------- %
  73.  
  74. % Copy one file to another.
  75. % Close the input file, but not the output file.
  76. /copyfile        % <infile> <outfile> copyfile <outfile> <length>
  77.  { 0 mark statbuf
  78.     { 4 index 1 index readstring
  79.       exch 5 index 1 index writestring
  80.       length 5 -1 roll add 4 1 roll
  81.       not { exit } if (.) print flush
  82.     } loop
  83.    cleartomark 3 -1 roll closefile dup == flush
  84.  } bind def
  85.  
  86. % Represent a Type 1 font in its most compressed format.
  87. % Requires -dWRITESYSTEMDICT to run.
  88. % Does not close the output file.
  89. (wrfont.ps) run
  90. /compressfont        % <fontname> <outfile> compressfont <outfile>
  91.  { exch save
  92.    systemdict /executeonly /readonly load put
  93.    systemdict /noaccess /readonly load put
  94.    systemdict readonly pop
  95.    wrfont_dict begin
  96.      /binary_CharStrings true def
  97.      /binary_tokens true def
  98.      /encrypt_CharStrings false def
  99.      /standard_only false def
  100.      /use_lenIV 0 def
  101.      /smallest_output true def
  102.    end
  103.    exch findfont setfont
  104.    1 index writefont
  105.    restore
  106.  } bind def
  107.  
  108. % ---------------- Main program ---------------- %
  109.  
  110. % Find the length of a file.
  111. /filelength        % <filename> filelength <length>
  112.  { status { pop pop exch pop } { -1 } ifelse
  113.  } bind def
  114.  
  115. % Define the header string for a compressed file.
  116. /beginfilestring
  117. ({dup token pop exch[/MaxCodeLength 2 index token pop/Tables 4 index token pop
  118. /EndOfData true/EncodeZeroRuns 256 .dicttomark
  119. /BoundedHuffmanDecode filter/MoveToFrontDecode filter
  120. [/BlockSize 4 -1 roll .dicttomark/BWBlockSortDecode filter
  121. }) readonly def
  122.  
  123. % Write a 16-bit big-endian non-negative integer on a file.
  124. /write16        % <file> <int> write16 -
  125.  { 2 copy -8 bitshift write 255 and write
  126.  } bind def
  127.  
  128. % Compress a group of files together.
  129. % Return a dictionary in which the keys are the input file names
  130. % and the values are [startpos length] in the uncompressed concatenation.
  131. % Does not open or close the output file.
  132. /tempname (t.em) def
  133. /packfiles        % <filenames> <outfile> packfiles <outfile> <posdict>
  134.  {    % Concatenate files to a temporary file.
  135.    tempname (w) file
  136.    dup /MoveToFrontEncode filter
  137.    dup <<
  138.     /BlockSize 1000000
  139.    >> /BWBlockSortEncode filter
  140.         % Stack: filenames outfile tempfile mtfe bwe
  141.    5 -1 roll dup length dict 0 6 2 roll
  142.     {        % Stack: outfile posdict pos tempfile mtfe bwe infilename
  143.       dup ==only dup (r) file 2 index copyfile exch pop
  144.       dup 7 index 4 2 roll 7 index exch 2 array astore put
  145.       5 -1 roll add 4 1 roll
  146.     } forall
  147.    closefile closefile closefile pop exch
  148.         % Stack: posdict outfile
  149.     % Compute an optimal Huffman code.
  150.    initstats
  151.    tempname (r) file addstats
  152.     % Actually compress the file.
  153.     % Write the decompression information on the output first.
  154.    dup tempname filelength write==
  155.    dup maxcode write==
  156.     % Write the code table as a homogenous number array.
  157.    statcodes exch
  158.      dup 149 write dup 32 write dup 2 index length write16
  159.      exch { 2 copy write16 pop } forall
  160.    dup <<
  161.     /MaxCodeLength maxcode
  162.     /EndOfData true
  163.     /EncodeZeroRuns 256
  164.     /Tables statcodes
  165.    >> /BoundedHuffmanEncode filter
  166.    tempname (r) file exch copyfile pop closefile
  167.    exch
  168.  } bind def
  169.  
  170. % Squeeze a font to a .cpf file in anticipation of compression.
  171. /squeezefont        % <fontname> squeezefont <filename.cpf>
  172.  { dup type /nametype ne { cvn } if
  173.    dup
  174.     { dup type /stringtype eq { exit } if
  175.       Fontmap exch get
  176.     }
  177.    loop
  178.         % Stack: fontname filename
  179.    dup dup
  180.     { (.) search not { exit } if
  181.       exch pop exch 3 -1 roll pop
  182.     }
  183.    loop
  184.         % Stack: fontname filename noextname extension
  185.    exch
  186.     { (/) search not { (\\) search not { exit } if } if
  187.       pop pop
  188.     }
  189.    loop
  190.         % If the font extension is anything other than
  191.         % .pfa or .pfb, we assume it can't be rewritten
  192.         % using compressfont.
  193.         % Stack: fontname filename extension basename
  194.    (.cpf) concatstrings dup 5 1 roll (w) file
  195.         % Stack: outfilename fontname filename extension outfile
  196.    exch dup (pfa) eq exch (pfb) eq or
  197.         % Stack: outfilename fontname filename outfile bool
  198.     { exch pop compressfont
  199.     }
  200.     { 3 -1 roll pop
  201.       exch findlibfile pop exch pop
  202.       exch copyfile pop
  203.     }
  204.    ifelse closefile
  205.  } bind def
  206.  
  207. % ---------------- Production code ---------------- %
  208.  
  209. % The following code constructs a packed version of the commercial-quality
  210. % fonts available from Aladdin Enterprises.  To use this code:
  211. %    - If desired, change the output file names below.
  212. %    - Make sure you have the synthetic font data (fontmap.shs and the
  213. %      *.ps files for the commercial fonts) in a directory that is on
  214. %      Ghostscript's search path.
  215. %    - Construct the packed fonts by running
  216. %        gs -dNODISPLAY -dWRITESYSTEMDICT packfile.ps
  217. %    - If desired, move the output files to the directory that will be
  218. %      used at run time.  You no longer need the *.pfb or *.ps files
  219. %      for the original fonts; however, you do still need the Fontmap
  220. %      for these fonts, because it defines the font name aliases.
  221. %    - Add the following line to the end of gs_fonts.ps:
  222. %        (ALL.cmp) run
  223. %      (substituting the definition of allmapname if you changed it).
  224.  
  225. % Define the output file names.  The extensions are arbitrary;
  226. % any legal file name is allowed.
  227. /allname (ALL.cff) def        % the compressed font file
  228. /allmapname (ALL.cmp) def    % the Fontmap override file
  229.  
  230. % Load an alternate Fontmap that references the synthetic oblique and
  231. % narrow fonts.
  232. true .setglobal
  233. (fontmap.shs) findlibfile pop exch pop .loadFontmap
  234. false .setglobal
  235.  
  236. % Define the packaging of fonts into font groups.
  237. % Fewer larger groups compress better, but make decompression slower.
  238. /Lists [
  239. [    % The oblique and narrow fonts are synthetic,
  240.     % and take very little space.
  241.   /AvantGarde-BookOblique /AvantGarde-DemiOblique
  242.   /Courier-Oblique /Courier-BoldOblique
  243.   /Helvetica-Oblique /Helvetica-BoldOblique
  244.   /Helvetica-Narrow /Helvetica-Narrow-Oblique
  245.   /Helvetica-Narrow-Bold /Helvetica-Narrow-BoldOblique
  246. ]
  247. [/AvantGarde-Book /AvantGarde-Demi
  248.  /Bookman-Light] [/Bookman-LightItalic
  249.  /Bookman-Demi /Bookman-DemiItalic
  250.  /Courier] [/Courier-Bold
  251.  /Helvetica /Helvetica-Bold]
  252. [/NewCenturySchlbk-Roman /NewCenturySchlbk-Italic
  253.  /NewCenturySchlbk-Bold /NewCenturySchlbk-BoldItalic]
  254. [/Palatino-Roman /Palatino-Italic
  255.  /Palatino-Bold /Palatino-BoldItalic]
  256. [/Times-Roman /Times-Italic
  257.  /Times-Bold /Times-BoldItalic]
  258. [/Symbol
  259.  /ZapfChancery-MediumItalic
  260.  /ZapfDingbats]
  261. ] def
  262.  
  263. % We need to register the fonts under their true names, not aliases.
  264. /Lists Lists mark exch
  265.  { mark exch
  266.     {  { Fontmap 1 index get dup type /nametype ne { pop exit } if
  267.      exch pop
  268.        }
  269.       loop
  270.     }
  271.    forall ]
  272.  }
  273. forall ] def
  274.  
  275. % Squeeze the fonts to their .cpf format.
  276. (Squeezing... ) print flush
  277. /fdict mark
  278. Lists
  279.  { { dup squeezefont } forall } forall
  280. .dicttomark def
  281. (done.\n) print flush
  282.  
  283. % Invert fdict.
  284. /f2dict fdict length dict def
  285. fdict { exch f2dict 3 1 roll put } forall
  286.  
  287. % Construct the compressed font file.
  288. (Creating ) print allname print (... ) print flush
  289. /posdict fdict length dict def
  290. /all allname (w) file def
  291. all beginfilestring writestring
  292. Lists
  293.  { dup == flush
  294.    /fbegin all fileposition def
  295.    mark exch { fdict exch get } forall ]
  296.    all packfiles exch pop
  297.    /flength all fileposition fbegin sub def
  298.     { fbegin flength 3 -1 roll aload pop 4 packedarray
  299.       exch f2dict exch get exch posdict 3 1 roll put
  300.     }
  301.    forall
  302.  }
  303. forall
  304. all closefile
  305. (done.\n) print flush
  306.  
  307. % Write the Fontmap addendum for accessing compressed fonts.
  308. (Writing ) print allmapname print (... ) print flush
  309. allmapname (w) file
  310. dup (%!
  311. /.runpackedlibfile where{pop}{(gs_pfile.ps)runlibfile}ifelse
  312. .currentglobal true .setglobal
  313. ) writestring
  314. posdict
  315.  { exch 2 index exch write==only exch dup ({) writestring
  316.    dup allname write==only
  317.    exch { 1 index dup ( ) writestring exch write==only } forall
  318.    dup ( .runpackedlibfile}bind .definefontmap
  319. ) writestring
  320.  }
  321. forall
  322. dup (.setglobal
  323. ) writestring
  324. closefile
  325. (done.\n) print flush
  326.